from django.shortcuts import render
from .models import Welcome
from django.http import JsonResponse
# Create your views here.

# 广告接口
def welcome(request):
    res = Welcome.objects.all().order_by('-order').first()   
    img = 'http://127.0.0.1:8000/media/' + str(res.img)
    print(img)
    return JsonResponse({'code': 100, 'msg': '成功', 'result': img})


###### views.py#########
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from .models import Banner, Notice
from .serializer import BannerSerializer, NoticeSerializer,CollectionSerializer,CollectionSaveSerializer
from rest_framework.mixins import DestroyModelMixin
class BannerView(GenericViewSet, ListModelMixin):
    queryset = Banner.objects.filter(is_delete=False).order_by('order')[:3]
    serializer_class = BannerSerializer

    def list(self, request, *args, **kwargs):
        res = super().list(request, *args, **kwargs)
        # 获取最后一条通知
        notice = Notice.objects.all().order_by('-create_time').first()
        serializer = NoticeSerializer(instance=notice)

        return Response({'code': 100, 'msg': '成功', 'banner': res.data, 'notice': serializer.data})
    

from .models import Collection
from datetime import datetime
class CollectionView(GenericViewSet,ListModelMixin,DestroyModelMixin):
    # 查出当天的--》没过滤当前用户
    queryset = Collection.objects.all().filter(create_time__gte=datetime.now().date())
    serializer_class = CollectionSerializer

    def list(self, request, *args, **kwargs):
        res=super().list(request, *args, **kwargs)
        today_count=len(self.get_queryset())
        return Response({'code':100,'msg':'成功','result':res.data,'today_count':today_count})
    


from .models import Area
from .serializer import AreaSerializer
class AreaView(GenericViewSet,ListModelMixin):
    queryset = Area.objects.all()
    serializer_class = AreaSerializer

#############views.py############
from .models import Collection
from datetime import datetime
from rest_framework.mixins import DestroyModelMixin,CreateModelMixin
class CollectionView(GenericViewSet,ListModelMixin,DestroyModelMixin,CreateModelMixin):
    # 查出当天的--》没过滤当前用户
    queryset = Collection.objects.all().filter(create_time__gte=datetime.now().date())
    serializer_class = CollectionSerializer

    def get_serializer_class(self):
        if self.action=='create':
            return CollectionSaveSerializer
        else:
            return CollectionSerializer
    def list(self, request, *args, **kwargs):
        res=super().list(request, *args, **kwargs)
        today_count=len(self.get_queryset())
        return Response({'code':100,'msg':'成功','result':res.data,'today_count':today_count})
    

    # 删除ss
    def destroy(self, request, *args, **kwargs):
        from libs.baidu_ai import BaDuFace
        instance = self.get_object()
        # 百度ai中删除
        baidu=BaDuFace()
        res=baidu.delete(instance.name_pinyin,instance.face_token)
        print(res)
        self.perform_destroy(instance)
        return Response()

